// pages/giftscene/giftscene.js 756
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var app = getApp()
var winScale = 750 / app.data.deviceInfo.windowWidth
Page({

  /**
   * 页面的初始数据
   */
  data: {
    set_id: '',
    scrollTop: 0,
    set_map_id: 0,
    currentTab: 0,
    page: 1,
    page_size: 20,
    stopScrolll: false,
    mapscurrentTab: 0,
    mapsfoldflag: 0,
    mapsfoldboxfixed: 0,
    filterConflag: false,
    showLoading: true,
    scenescrollTop: 0,
    goodsflag: true,
    hasMore: true,
    giftsetlistarr: [],
    pointmoveX: 0,
    mapspriceflag: 0,
    mapsnavbarFa: '642',
    start_price: 0,
    end_price: 0,
    maxpriceflag: true,
    mapssearch: 0,
    mapspricelineflag: false //当没有商品且不是由于搜素造成的无商品时，价格筛选需要隐藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //giftsetlist
    var pages = getCurrentPages();
    if (pages.length >= 2) {
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        promotefresh: false
      })
    }
    this.setData({
      set_id: options.sceneid,
      currentTab: Number(options.index)
    })
    this.giftsetlist()
  },
  giftsetlist: function(start, end) {
    var that = this;
    var start_price = start || 0;
    var end_price = end || 0;
    shihan.sendRequest(config.giftsetlist + that.data.set_id + '_' + that.data.set_map_id + '/' + start_price + '_' + end_price + '/' + that.data.page + '-' + that.data.page_size + '.html', 'GET', {})
      .then(function(response) {
        // console.log(response)
        if (response.data.code == 0) {
          if (response.data.data.sh_set_maps) {
            if (response.data.data.sh_set_maps.length <= 3) {
              that.setData({
                mapslenflag: 1,
                mapsnavbarFa: 722
              })
            } else {
              that.setData({
                mapslenflag: 0,
                mapsnavbarFa: 642
              })
            }
            response.data.data.sh_set_maps.unshift({
              sh_id: 0,
              sh_name: "全部"
            });
          }
          if (response.data.data.sh_rem_product.length > 0) {
            for (var i = 0; i < response.data.data.sh_rem_product.length; i++) {
              if (response.data.data.sh_rem_product[i].sh_label) {
                if (response.data.data.sh_rem_product[i].sh_label.sh_location) {
                  var pos = response.data.data.sh_rem_product[i].sh_label
                  pos['posa'] = pos.sh_location.split('_')[0];
                  pos['posb'] = pos.sh_location.split('_')[1];
                }
              }
            }
            that.setData({
              hasMore: true,
              showLoading: false,
              page: that.data.page + 1,
              goodsflag: true,
              mapspricelineflag: false,
              giftsetlistarr: that.data.giftsetlistarr.concat(response.data.data.sh_rem_product),
            });
            if (that.data.end_price) {
              that.setData({
                end_price: that.data.end_price
              })
            } else {
              that.setData({
                max_price: response.data.data.sh_max_price
              })
            }
          } else {
            that.setData({
              hasMore: false,
              goodsflag: false,
              showLoading: false
            });
            if (that.data.mapssearch == 0) {
              that.setData({
                mapspricelineflag: true
              });
            }
          }
          if (response.data.data.sh_set) {
            that.setData({
              giftsetlist: response.data.data
            });
          }
        } else {
          that.setData({
            goodsflag: false,
            showLoading: false
          })
          if (that.data.mapssearch == 0) {
            that.setData({
              mapspricelineflag: true
            });
          }
        }
      }, function(error) {
        console.log(error);
      });
    console.log(that.data);
  },
  mapsonNavbarTap: function(e) { //送礼对象切换
    // console.log(e)
    var that = this;
    this.setData({
      page: 1,
      mapssearch: 0,
      start_price: 0,
      end_price: 0,
      startprice: '',
      endprice: '',
      maxpriceflag: true,
      giftsetlistarr: [],
      mapsfoldflag: 0,
      mapspricefixed: 0,
      showLoading: true,
      filterConflag: false,
      stopScrolll: false,
      mapsnavbarleft: ((e.currentTarget.dataset.index + 1) * 160 - 71 - 375) / winScale,
      mapscurrentTab: e.currentTarget.dataset.index,
      set_map_id: e.currentTarget.dataset.goodid
    });
    this.giftsetlist()
  },
  onNavbarTap: function(e) { //送礼场景切换
    // console.log(e)
    var that = this;
    if (this.data.filterConflag == false) {
      this.setData({
        page: 1,
        showLoading: true,
        mapssearch: 0,
        mapspricefixed: 0,
        mapscurrentTab: 0,
        giftsetlistarr: [],
        mapsnavbarleft: 0,
        set_map_id: 0,
        start_price: 0,
        end_price: 0,
        maxpriceflag: true,
        startprice: '',
        endprice: '',
        navbarleft: ((e.currentTarget.dataset.index + 1) * 180 - 90 - 375) / winScale,
        currentTab: e.currentTarget.dataset.index,
        set_id: e.currentTarget.dataset.goodid
      });
      this.giftsetlist()
    }
  },
  openmapsfold: function(e) {
    if (this.data.mapspriceflag == 1) {
      if (this.data.mapsfoldflag == 0) {
        this.setData({
          mapspricefixed: 1,
          mapspriceflag: 0,
          mapsnavbarFa: '642'
        })
      } else {
        this.setData({
          mapsfoldflag: 0,
          mapspricefixed: 0
        })
      }
    } else {
      if (this.data.mapsfoldflag == 0) {
        this.setData({
          mapsfoldflag: 1,
          filterConflag: true,
          stopScrolll: true
        })
      } else {
        this.setData({
          mapsfoldflag: 0,
          stopScrolll: false,
          filterConflag: false
        })
      }
    }
  },
  otherclosefilter: function() {
    this.setData({
      mapsfoldflag: 0,
      stopScrolll: false,
      filterConflag: false
    })
  },
  numbergit: function(e) {
    var leveal = e.currentTarget.dataset.leveal;
    if (leveal == 'low') {
      this.setData({
        start_price: e.detail.value
      })
    } else if (leveal == 'height') {
      this.setData({
        end_price: e.detail.value
      })
    }
    // console.log(this.data)
  },
  comfirmprice: function() {
    // if (this.data.start_price && this.data.end_price){
    if (!this.data.start_price) {
      this.setData({
        startprice: '',
        start_price: 0,
      })
    } else {
      this.setData({
        startprice: this.data.start_price,
        start_price: this.data.start_price
      })
    }
    if (!this.data.end_price) {
      this.setData({
        endprice: '',
        end_price: 0
      })
    } else {
      this.setData({
        endprice: this.data.end_price,
        end_price: this.data.end_price
      })
    }
    if (this.data.start_price && !this.data.end_price) {
      this.setData({
        maxpriceflag: false
      })
    }
    if (!this.data.start_price && !this.data.end_price) {
      this.setData({
        maxpriceflag: true
      })
    }
    this.setData({
      page: 1,
      mapssearch: 1,
      showLoading: true,
      mapspricefixed: 0,
      giftsetlistarr: []
    });
    if (this.data.giftsetlistarr.length == 0) {
      this.giftsetlist(this.data.start_price, this.data.end_price)
    }
    // }
  },
  onScroll: function(ev) {
    if (this.data.giftsetlist.sh_themes) {
      var length = this.data.giftsetlist.sh_themes.length;
    } else {
      var length = 0;
    }
    var scrollheight = (756 * length + 340 + 98) / winScale;
    if (length == 0) {
      var scrollheight = 340 / winScale;
    }
    var scrollTop = ev.detail.scrollTop;
    this.setData({
      scenescrollTop: scrollTop
    })
    if (scrollTop >= (340 / winScale)) {
      this.setData({
        mapsfoldboxfixed: 1
      });
    } else {
      this.setData({
        mapsfoldboxfixed: 0
      });
    }
    if (length > 0) {
      if (scrollTop >= scrollheight) {
        this.setData({
          mapspriceflag: 1,
          mapsnavbarFa: '562',
          mapspricefixed: 0
        });
      } else {
        this.setData({
          mapspriceflag: 0,
          mapsnavbarFa: '642'
        });
      }
    } else {
      if (scrollTop >= (340 / winScale)) {
        this.setData({
          mapspriceflag: 1,
          mapsnavbarFa: '562',
          mapspricefixed: 0
        });
      } else {
        this.setData({
          mapspriceflag: 0,
          mapsnavbarFa: '642'
        });
      }
    }
    if (scrollTop > 200) {
      this.setData({
        backToTop: true
      });
    } else {
      this.setData({
        backToTop: false
      });
    }

  },
  viewgoodsDetail: function(e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },
  goodsloadMoreData: function() {
    if (this.data.goodsflag) {
      this.setData({
        goodsflag: false,
        mapssearch: 1
      })
      this.giftsetlist(this.data.start_price, this.data.end_price);
    }
  },
  handlerMove(e) {
    let moveX = e.touches[0].clientX;
    console.log(moveX)
    this.setData({
      pointmoveX: moveX / 10
    })
    // let { list } = this.data;
    // let moveY = e.touches[0].clientY;
    // let rY = moveY - this.offsetTop;
    // if (rY >= 0) {
    //   
    //   let index = Math.ceil((rY - this.apHeight) / this.apHeight);
    //   console.log(index)
    //   if (0 <= index < list.length) {
    //     let nonwAp = list[index];
    //     nonwAp && this.setData({ alpha: nonwAp.sh_zimu });
    //   }
    // }
  },
  openstrategyinfo: function(e) {
    if (e.currentTarget.dataset.url) {
      wx.navigateTo({
        url: '/pages/strategyinfo/strategyinfo?url=' + encodeURIComponent(e.currentTarget.dataset.url)
      })
    }
  },
  opengoodsdetail: function(e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})